# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""
# File       : change.py
# Time       ：2025/5/27 17:15
# Description：
"""
import os
import time
import uuid
from fastapi import Request
from fastapi import APIRouter, status, BackgroundTasks
from fastapi.responses import JSONResponse
from apps.mock.models.change import GetCid, BeginCharge, EndCharge
from utils.log import log

charge = APIRouter(
    prefix='/v1/third'
)


class Customer:
    def __init__(self):
        self.cid = None
        self.left_duration = None

    def get_cid(self, cid):
        self.cid = cid

    def update_duration(self, left_duration):
        self.left_duration = left_duration


customer = Customer()


@charge.post("/getchargeid")
async def get_cid(request: Request):
    request_id = str(uuid.uuid4())
    log.info(f"received getchargeid req: {request.json()}")
    duration = os.getenv('CID_DURATION', 400)

    cid = str(int(time.time()))
    resp = {
        "ret": {
            "code": 0,
            "msg": "ok",
            "request_id": request_id
        },
        "charging_info": {
            "cid": cid,
            "left_duration": duration
        }
    }
    customer.get_cid(cid)
    customer.update_duration(duration)
    log.info(f"getchargeid resp: {resp}")
    return JSONResponse(
        content=resp, status_code=status.HTTP_200_OK, headers={"Content-Type": "application/json"}
    )


@charge.post("/begincharging")
async def start_charge(request: Request):
    request_id = str(uuid.uuid4())
    log.info(f"received begincharging req: {request.json()}")
    cid = customer.cid
    duration = customer.left_duration
    resp = {
        "ret": {
            "code": 0,
            "msg": "ok",
            "request_id": request_id
        },
        "charging_info": {
            "cid": cid,
            "left_duration": duration
        }
    }
    log.info(f"begincharging resp: {resp}")
    return JSONResponse(
        content=resp, status_code=status.HTTP_200_OK, headers={"Content-Type": "application/json"}
    )


@charge.post("/endcharging")
def end_charge(request: Request):
    request_id = str(uuid.uuid4())
    log.info(f"received endcharging req: {request.json()}")
    resp = {
        "ret": {
            "code": 0,
            "msg": "ok",
            "request_id": request_id
        }
    }
    log.info(f"endcharging resp: {resp}")
    return JSONResponse(
        content=resp, status_code=status.HTTP_200_OK, headers={"Content-Type": "application/json"}
    )
